package com.program.facesigninsystem.util;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author sxw
 */
public class FileUtil {
    private static Logger log = Logger.getLogger(FileUtil.class.toString());

    public static File loadFile(String filePath, String createTime,MultipartFile list,String fileName){
        filePath = filePath + createTime + fileName;
        System.out.println(filePath);
        File file = new File(filePath);
        try {
            if (!file.getParentFile().exists()) {
                if(!file.getParentFile().mkdir()){
                    System.out.println("文件名不存在");
                }
            }
            if (!file.exists()) {
                if(!file.createNewFile()) {
                    return null;
                }
            }
            list.transferTo(file);
        } catch (IOException ioe) {
            log.log(Level.SEVERE,"Error parsing date",ioe);
            if(!file.delete()){
                return null;
            }
        }
        return file;
    }
}
